const express = require("express");
const router = express.Router();
const Response = require("../utils/Response.js");
const fs = require("fs");

// 引入mysql连接池
const pool = require("../utils/db.js");


// 新增课程

router.post("/add", (req, res, next) => {
  let obj = req.body;
  pool.query("insert into course_project set ?", [obj], (err, result) => {
    if (err) {
      return next(err);
    }
    console.log(result);
    if(result.affectedRows===0){
      res.send({code:201,msg:'新增失败'})
    }else{
      res.send({ code: 200, msg: '新增成功' })
    }
  });
});

// 查询所有课程类型
router.get("/type", (req, res, next) => {
  let obj = req.query;
  console.log(obj);
  pool.query("select * from course_name", [obj], (err, result) => {
    if (err) {
      return next(err);
    }
    console.log(result);
    //结果是对象，如果对象下的属性length为0说明修改失败
    if (result.length === 0) {
      res.send({ code: 201, msg: "查询失败" });
    } else {
      res.send({ code: 200, msg: "查询成功", data: result });
    }
  });
});

// 通过类型查询有关课程
router.get("/project", (req, res, next) => {
  let obj = req.query;
  console.log(obj);
  pool.query(
    "select * from course_project where c_id=?",
    [obj.c_id],
    (err, result) => {
      if (err) {
        return next(err);
      }
      console.log(result);
      //结果是对象，如果对象下的属性length为0说明修改失败
      if (result.length === 0) {
        res.send({ code: 201, msg: "查询失败" });
      } else {
        res.send({ code: 200, msg: "查询成功", data: result });
      }
    }
  );
});

// 通过pid查询有关课程
router.get("/byId", (req, res, next) => {
  let obj = req.query;
  console.log(obj);
  pool.query(
    "select * from course_project where pid=?",
    [obj.pid],
    (err, result) => {
      if (err) {
        return next(err);
      }
      console.log(result);
      //结果是对象，如果对象下的属性length为0说明修改失败
      if (result.length === 0) {
        res.send({ code: 201, msg: "查询失败" });
      } else {
        res.send({ code: 200, msg: "查询成功", data: result });
      }
    }
  );
});

// 通过uid查找课程
router.get("/byuid", (req, res, next) => {
  let obj = req.query;
  console.log(obj);
  pool.query(
    "select course_project.*,user_course.id from course_project,user_course where uid=? and course_id=pid",
    [obj.uid],
    (err, result) => {
      if (err) {
        return next(err);
      }
      console.log(result);
      //结果是对象，如果对象下的属性length为0说明修改失败
      if (result.length === 0) {
        res.send({ code: 201, msg: "查询失败" });
      } else {
        res.send({ code: 200, msg: "查询成功", data: result });
      }
    }
  );
});

// 用户删除课程信息
router.post('/delete_user', (req, res, next) => {
	let obj = req.body;
	console.log(obj);
		pool.query('delete from user_course where ?', [obj], (err, result) => {
			if (err) {
				return next(err);
			}
			else {
        if(result.affectedRows===0){
          res.send({code:201,msg:'删除失败'})
        }else{
          res.send({ code: 200, msg: '删除成功' })
        }
			}
	});
});


// 查询所有课程
router.get("/project/all", (req, res, next) => {
  let obj = req.query;
  console.log(obj);
  pool.query(
    "select * from course_project",
    [obj],
    (err, result) => {
      if (err) {
        return next(err);
      }
      console.log(result);
      //结果是对象，如果对象下的属性length为0说明修改失败
      if (result.length === 0) {
        res.send({ code: 201, msg: "查询失败" });
      } else {
        res.send({ code: 200, msg: "查询成功", data: result });
      }
    }
  );
});

// 课程的介绍
router.get("/introduction", (req, res, next) => {
  let obj = req.query;
  console.log(obj);
  pool.query(
    "select course_project_introduction.* from course_project_introduction,course_project where ? and project_id=pid",
    [obj],
    (err, result) => {
      if (err) {
        return next(err);
      }
      console.log(result);
      //结果是对象，如果对象下的属性length为0说明修改失败
      if (result.length === 0) {
        res.send({ code: 201, msg: "查询失败" });
      } else {
        res.send({ code: 200, msg: "查询成功", data: result });
      }
    }
  );
});

// 通过关键字查找课程
router.post("/project", (req, res, next) => {
  let { title } = req.body;
  console.log(title);
  pool.query(
    "select * from course_project where title like ?",
    [`%${title}%`],
    (err, result) => {
      if (err) {
        return next(err);
      }
      console.log(result);
      if (result.length === 0) {
        res.send({ code: 201, msg: "不存在" });
      } else {
        res.send({ code: 200, msg: "查询成功", data: result });
      }
    }
  );
});

// 删除课程信息
router.post('/delete', (req, res, next) => {
	let obj = req.body;
	console.log(obj);
		pool.query('delete from course_project where pid=?', [obj.pid], (err, result) => {
			if (err) {
				return next(err);
			}
			else {
        if(result.affectedRows===0){
          res.send({code:201,msg:'删除失败'})
        }else{
          res.send({ code: 200, msg: '删除成功' })
        }
			}
	});
});

// 修改课程信息
router.post('/update', (req, res, next) => {
	let obj = req.body;
	console.log(obj);
		pool.query('update course_project set title=?,duration=?,pic=?,video=? where pid=?', [obj.title,obj.duration,obj.pic,obj.video,obj.pid], (err, result) => {
			if (err) {
				return next(err);
			}
			else {
        if(result.affectedRows===0){
          res.send({code:201,msg:'修改失败'})
        }else{
          res.send({ code: 200, msg: '修改成功' })
        }
			}
	});
});

// 查询课程对应的教练
router.get('/course_coach', (req, res, next) => {
  let obj = req.query;
  console.log(obj);
  pool.query('select coach.*,course_project.* from coach,course_project where coach_id=coa_id', [obj], (err, result) => {
    if (err) {
      return next(err);
    }
    console.log(result);
    if(result.length===0){
      res.send({code:201,msg:'查询失败'})
    }else{
      res.send({ code: 200, msg: '查询成功', data: result })
    }
  });
})

// 查询课程类型对应的课程教练
router.get('/course_type_id', (req, res, next) => {
  let obj = req.query;
  console.log(obj);
  pool.query('select coach.*,course_project.* from coach,course_project where c_id=? and coach_id=coa_id', [obj.c_id], (err, result) => {
    if (err) {
      return next(err);
    }
    console.log(result);
    if(result.length===0){
      res.send({code:201,msg:'查询失败'})
    }else{
      res.send({ code: 200, msg: '查询成功', data: result })
    }
  });
})


// 查询课程详情

router.get("/detail", (req, res, next) => {
  let obj = req.query;
  console.log(obj);
  pool.query(
    "select course_project_detail.*,course_project.title,course_project.duration,course_project.pic,course_project.detail from course_project_detail,course_project where course_project.pid=? and course_project.pid=course_project_detail.pid",
    [obj.pid],
    (err, result) => {
      if (err) {
        return next(err);
      }
      console.log(result);
      //结果是对象，如果对象下的属性length为0说明修改失败
      if (result.length === 0) {
        res.send({ code: 201, msg: "查询失败" });
      } else {
        res.send({ code: 200, msg: "查询成功", data: result });
      }
    }
  );
});


// 查询课程训练
router.get("/train", (req, res, next) => {
  let obj = req.query;
  console.log(obj);
  pool.query(
    "select * from course_project_train where ?",
    [obj],
    (err, result) => {
      if (err) {
        return next(err);
      }
      console.log(result);
      //结果是对象，如果对象下的属性length为0说明修改失败
      if (result.length === 0) {
        res.send({ code: 201, msg: "查询失败" });
      } else {
        res.send({ code: 200, msg: "查询成功", data: result });
      }
    }
  );
});

module.exports = router;
